﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Fc
{
    public partial class YuanLiao_XiaoHao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
            if (ViewState["SelectCommand"] == null)
            {
                SetSql();
            }
            else
            {
                SqlDataSource1.SelectCommand = (string)ViewState["SelectCommand"];
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
        }


        protected void Button_Save_Click(object sender, EventArgs e)
        {
            float f = 0;
            float.TryParse(this.PeiHeBi.Text, out f);
            if (f <= 0)
            {
                this.Message.Text = "·消耗量输入有误";
                return;
            }
            DateTime d;
            if(!DateTime.TryParse(this.RiQi.Text,out d))
            {
                this.Message.Text = "·日期输入有误";
                return;
            }
            this.SqlDataSource3.UpdateCommand = "if not exists(select * from [分厂原料消耗] where [分厂ID]="
                + this.FenChang.Text + " and [原料ID]=" + this.YuanFuLiao.Text 
                + " and [日期]='"+d.ToShortDateString()+"')"
                + " begin insert into [分厂原料消耗]([分厂ID],[原料ID],[日期],[数量])values("
                + this.FenChang.Text + "," + this.YuanFuLiao.Text + ",'" + d.ToShortDateString() + "'," + f + ") end";

            if (this.SqlDataSource3.Update() == 1)
            {
                this.Message.Text = "·录入成功";
                //this.Panel2.Visible = false;
                SetSql();
            }
            else
            {
                this.Message.Text = "·数据已经存在，录入失败";
            }


        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            Panel2.Visible = false;
        }

        protected void FenChang_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetSql();
        }

        protected void RiQi_TextChanged(object sender, EventArgs e)
        {
            SetSql();
        }
        private void SetSql()
        {
            DateTime d;
            if (!DateTime.TryParse(this.RiQi.Text, out d))
            {
                this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                d = DateTime.Today;
            }
            if (this.FenChang.Text == "")
            {
                this.FenChang.DataBind();
            }
            SqlDataSource1.SelectCommand =
@"select [分厂原料消耗].[ID]
,[生产单位].[名称] as [分厂名称]
,[分厂原料消耗].[日期]
,[原料].[品名]+'('+[原料].[规格]+')' as [原料]
,[原料].[单位] as [原料单位]
,[分厂原料消耗].[数量]
from [分厂原料消耗],[生产单位],[原料]
where [分厂原料消耗].[分厂ID]=[生产单位].[ID]
and [分厂原料消耗].[原料ID]=[原料].[ID]
and [分厂原料消耗].[日期]="
+ "'" + d.ToShortDateString() + "'"
+ " and [分厂原料消耗].[分厂ID]=" + FenChang.Text
;
            ViewState["SelectCommand"] = SqlDataSource1.SelectCommand;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            SetSql();
        }

    }
}
